package com.itheima.mobilesafe69.activity;

import java.io.File;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.activity.AppLockActivity.MyAdapter;
import com.itheima.mobilesafe69.global.MyApplication;
import com.itheima.mobilesafe69.utils.SmsUtil;
import com.itheima.mobilesafe69.utils.SmsUtil.SmsCallBack;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AToolActivity extends Activity {
	private ProgressBar pb_bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_atool);
		
		//全局异常的捕获
//		int i = 10/0;
		
		MyApplication myApplication = (MyApplication) getApplication();
		myApplication.test();
		
		//查询归属方法
		initQueryAddress();
		//备份短信
		initSmsBackUp();
		//开启常用号码的方法
		initCommonNumber();
		//程序锁
		initAppLock();
	}

	private void initAppLock() {
		TextView tv_applock = (TextView) findViewById(R.id.tv_applock);
		tv_applock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),AppLockActivity.class));
			}
		});
	}

	private void initCommonNumber() {
		TextView tv_common_number_query = (TextView) findViewById(R.id.tv_common_number_query);
		tv_common_number_query.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),CommonNumberActivity.class));
			}
		});
	}

	private void initSmsBackUp() {
		TextView tv_sms_backup = (TextView) findViewById(R.id.tv_sms_backup);
		pb_bar = (ProgressBar) findViewById(R.id.pb_bar);
		tv_sms_backup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//弹出一个对话框,显示短信备份进度
				showProgressDialog();
			}
		});
	}

	protected void showProgressDialog() {
		final ProgressDialog progressDialog = new ProgressDialog(this);
		progressDialog.setTitle("短信备份");
		//默认的进度条是圆型转圈,手动指定成水平
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		//弹出对话框,然后在子线程中去对对话框的百分比做修改
		progressDialog.show();
		
		//短信备份逻辑
		new Thread(){
			public void run() {
				//短信备份方法,指定到sd中,sd卡路径,状态
				if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
					String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"sms69.xml";
//					SmsUtil.backUp(sdPath,AToolActivity.this,pb_bar);
					SmsUtil.backUp(sdPath,getApplicationContext(),new SmsCallBack() {
						@Override
						public void setProgress(int progress) {
							progressDialog.setProgress(progress);
							pb_bar.setProgress(progress);
						}
						
						@Override
						public void setMax(int max) {
							progressDialog.setMax(max);
							pb_bar.setMax(max);
						}
					});
					
					//消失对话框,可以在子线程中执行,设置进度条
					progressDialog.dismiss();
				}
			};
		}.start();	
	}

	private void initQueryAddress() {
		TextView tv_address_query = (TextView) findViewById(R.id.tv_address_query);
		tv_address_query.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//开启一个新的界面做电话号码输入查询操作
				startActivity(new Intent(getApplicationContext(), QueryAddresssActivity.class));
			}
		});
	}
}
